import {
  IModel,
  SPI,
  TableWidget,
  BaseElementWidget,
  getModelByUrl,
  getModel,
  Widget,
  queryPage,
  queryOne,
  CustomWidget,
  http,
  IModelField,
  insertOne,
  buildSingleItemParam,
  ModelFieldType,
  callFunction,
  callFun,
  executeViewAction,
  RedirectTargetEnum,
  RuntimeViewAction,
  ViewActionTarget,
  ViewType,
  ViewMode
} from '@kunlun/mobile-dependencies';

import H5Designer from './H5Designer.vue';
@SPI.ClassFactory(CustomWidget.Token({ widget: 'custom-home-config' }))
export class CustomHomeConfigWidget extends TableWidget {
  public initialize(props) {
    super.initialize(props);
    this.setComponent(H5Designer);
    return this;
  }

  @Widget.Reactive()
  protected configData: Record<string, unknown>[] = [];

  protected async mounted() {
    super.mounted();

    this.loadData();
  }

  // 请求首页栏目数据
  @Widget.Method()
  public async loadData() {
    // const data = await queryPage("demo.ScuColumnEntity", { pageSize: -1, currentPage: 1 }, undefined, {
    //   scene: 'DemoMenus_ScuMenu_ScuColumnEntityMenu'
    // }, { maxDepth: 2 });
    const data = await queryPage("demo.ScuColumnEntity", { pageSize: -1, currentPage: 1 }, undefined, {
      scene: 'DemoMenus_ScuMenu_ScuColumnEntityMenu'
    });
    console.log('loadData', data.content);
    this.configData = [];
    data.content.forEach((value, index, array) => {
      this.loadColumnContent(value);
    });
  }

  @Widget.Method()
  public async loadColumnContent(value) {
    console.log('loadColumnContent', value.id);
    const query = `
    query {
      scuColumnElementEntityQuery {
        queryColumn(columnId: "${value.id}") {
          id
          elementName
          columnType
          picUrl
          weight
          jumpType
          dataMode
          jumpUrl
          categoryIds
          detailId
          studentCount
          lecturerName
          lecturerCompany
          lecturerPosition
          paymentType
          originalPrice
          currentPrice
          functionModule
          articlePicType
          treasureChestSource
          treasureChestContent
          treasureChestTags
          columnId
          columnEntity {
            id
            columnName
          }
          createDate
          writeDate
          createUid
          writeUid
        }
      }
    }
    `;
    const result = await http.query('DemoCore', query);
    const content = result.data.scuColumnElementEntityQuery.queryColumn;
    console.log('loadColumnContent', content);
    const columnContent = content; // 栏目下的内容
    value.columnContent = columnContent;
    value.isShowRight = false;
    if (value.columnStyle === 1) {
      value.widgetName = 'QxbColumnKongList';
    } else if (value.columnStyle === 2) {
      // 图片轮播
      value.widgetName = 'QxbBannerWidget';
    } else if (value.columnStyle === 6) {
      // 标题图片横版（二）
      // 标题图片横版（二）
      if (value.weight) {
        console.log('value11111---', value);
        value.widgetName = 'QxbColumnListWidget';
      } else {
        value.widgetName = 'QxbColumnListWidget2';
      }
      // value.widgetName = "QxbColumnListWidget"
    } else if (value.columnStyle === 1) {
      // 金刚区
      value.widgetName = 'KongListFieldWidget';
    } else {
      value.widgetName = 'QxbBannerWidget';
    }
    this.configData.push(value);
    console.log('configData', this.configData);
  }

  @Widget.Method()
  public async columnClick(params) {
    //栏目详情http://localhost:88/page;module=DemoCore;model=demo.ScuColumnEntity;viewType=DETAIL;action=redirectDetailPage;menu=%7B%22selectedKeys%22:%5B%22DemoMenus_ScuMenu_ScuColumnEntityMenu%22%5D,%22openKeys%22:%5B%22DemoMenus_ScuMenu%22%5D%7D;scene=redirectDetailPage;menuRandomKey=1705041129084;id=559078268037404331;target=ROUTER
    //首页信息http://localhost:88/page;module=DemoCore;model=demo.DemoItem;viewType=TABLE;action=homepage;scene=homepage;target=OPEN_WINDOW
    //首页详情http://localhost:88/page;module=DemoCore;model=demo.DemoItem;viewType=DETAIL;action=redirectDetailPage;scene=redirectDetailPage;target=ROUTER;id=559078268037404322
    console.log("columnClick")
    console.log("actions", this.model.modelActions)
    const model = await getModel("demo.DemoItem");
    console.log(model)
    const action = (this.model.modelActions || []).find((action) => {
      return action.name === "redirectDetailPage"
    })
    console.log("action", action)
    executeViewAction(action as RuntimeViewAction , undefined, undefined , undefined , RedirectTargetEnum.SELF)
  }


}
